<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="演示视频的url" prop="videoUrl">
        <el-input v-model="queryParams.videoUrl" placeholder="请输入演示视频的url" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标杆产品的排序顺序" prop="top">
        <el-input v-model="queryParams.top" placeholder="请输入标杆产品的排序顺序" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="研发完成时间" prop="productTime">
        <el-input v-model="queryParams.productTime" placeholder="请输入研发完成时间" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="研发背景" prop="productBg">
        <el-input v-model="queryParams.productBg" placeholder="请输入研发背景" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品的源代码链接" prop="giteeUrl">
        <el-input v-model="queryParams.giteeUrl" placeholder="请输入产品的源代码链接" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:list:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:list:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:list:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:list:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="Uuid" align="center" prop="productId" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品类型" align="center" >
        <template slot-scope="scope">
          {{ statusMap[scope.row.productType] }}
        </template>
      </el-table-column>
      <el-table-column label="产品封面图片" align="center" prop="pictureUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pictureUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="演示视频的url" align="center" prop="videoUrl">
        <template slot-scope="scope">
          <video :width="200" :height="200" controls>
            <source :src="scope.row.videoUrl" type="video/mp4" />
            <source :src="scope.row.videoUrl" type="video/webm" />
            <source :src="scope.row.videoUrl" type="video/ogg" />
            您的浏览器不支持视频播放
          </video>
        </template>
      </el-table-column>
      <el-table-column label="系统的功能描述" align="center" prop="productDetails" />
      <el-table-column label="标杆产品的排序顺序" align="center" prop="top" />
      <el-table-column label="研发完成时间" align="center" prop="productTime" />
      <el-table-column label="研发背景" align="center" prop="productBg" />
      <el-table-column label="产品的源代码链接" align="center" prop="giteeUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:list:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:list:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改研发中心展示对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <el-input v-model="form.productType" placeholder="请输入产品类型（1,2,3）" />
        </el-form-item>
        <!-- 修改表单中的图片和视频上传部分 -->
        <el-form-item label="产品封面" prop="pictureUrl">
          <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false" :on-success="handleAvatarSuccess" :on-change="handleAvatarChange"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.pictureUrl" :src="form.pictureUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="演示视频" prop="videoUrl">
          <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false" :on-success="handleAvatarSuccess2" :on-change="handleAvatarChange2"
            :before-upload="beforeAvatarUpload2">
            <!-- 使用 <video> 标签来显示视频 -->
            <video v-if="form.videoUrl" controls :src="form.videoUrl" class="avatar"></video>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="系统的功能描述" prop="productDetails">
          <el-input v-model="form.productDetails" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="标杆产品的排序顺序" prop="top">
          <el-input v-model="form.top" placeholder="请输入标杆产品的排序顺序" />
        </el-form-item>
        <el-form-item label="研发完成时间" prop="productTime">
          <el-input v-model="form.productTime" placeholder="请输入研发完成时间" />
        </el-form-item>
        <el-form-item label="研发背景" prop="productBg">
          <el-input v-model="form.productBg" placeholder="请输入研发背景" />
        </el-form-item>
        <el-form-item label="产品的源代码链接" prop="giteeUrl">
          <el-input v-model="form.giteeUrl" placeholder="请输入产品的源代码链接" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listList, getList, delList, addList, updateList } from "@/api/system/list";

export default {
  name: "List",
  data() {
    return {
      avatarList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 研发中心展示表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productType: null,
        pictureUrl: null,
        videoUrl: null,
        productDetails: null,
        top: null,
        productTime: null,
        productBg: null,
        giteeUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  computed: {
    // 状态映射表
    statusMap() {
      return {
        1: 'WEB',
        2: 'APP',
        3: '小程序'
      };
    }
  },
  methods: {
    /** 查询研发中心展示列表 */
    getList() {
      this.loading = true;
      listList(this.queryParams).then(response => {
        this.listList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log()
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productName: null,
        productType: null,
        pictureUrl: null,
        videoUrl: null,
        productDetails: null,
        top: null,
        productTime: null,
        productBg: null,
        giteeUrl: null,
        avatarFile: null,
        videoFile: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加研发中心展示";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.form.productId =row.productId   
      this.reset();
      const productId = row.productId || this.ids
      getList(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改研发中心展示";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log(this.form)
        // 创建 FormData 对象
        const formData = new FormData();
        // 将表单中的其他数据添加到 FormData
        formData.append('productId', this.form.productId)
        formData.append('productName', this.form.productName);
        formData.append('productType', this.form.productType);
        formData.append('pictureUrl', this.form.pictureUrl);
        formData.append('videoUrl', this.form.videoUrl);
        formData.append('productDetails', this.form.productDetails);
        formData.append('top', this.form.top);
        formData.append('productTime', this.form.productTime)
        formData.append('productBg', this.form.productBg)
        formData.append('giteeUrl', this.form.giteeUrl)
        formData.append('avatarFile', this.form.avatarFile);
        formData.append('videoFile', this.form.videoFile);

        if (valid) {
          if (this.form.productId != null) {
            updateList(this.form).then(response => {
              console.log("修改成功")
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      console.log(productIds);
      this.$modal.confirm('是否确认删除研发中心展示编号为"' + productIds + '"的数据项？').then(function() {
        return delList(productIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/list/export', {
        ...this.queryParams
      }, `list_${new Date().getTime()}.xlsx`)
    },
    handlePictureUpload(response) {
      this.form.pictureUrl = response.url;
    },
    handleImageError(event) {
      // 设置图片加载失败时的默认图片
      event.target.src = '../../assets/images/avatar.jpg';
    },
    handleAvatarSuccess(response, file) {
      // 上传成功后的回调
      this.form.avatarFile = URL.createObjectURL(file.raw);
      this.form.pictureUrl = response.data.url; // 假设返回的response中有文件的URL
    },
    /** 上传前的处理 */
    beforeAvatarUpload(file) {
      const isImage = file.type.startsWith("image/");
      const isLt80M = file.size / 1024 / 1024 < 80; // 限制文件大小为 80MB

      if (!isImage) {
        this.$message.error("上传的文件必须是图片格式!");
      }

      if (!isLt80M) {
        this.$message.error("上传的图片大小不能超过 80MB!");
      }

      if (this.avatarList.length >= 1) {
        this.$message.error("只允许上传一张图片!");
        return false; // 阻止上传
      }

      return isImage && isLt80M;
    },
    /** 图片变化时的处理函数 */
    handleAvatarChange(file) {
      // 使用 URL.createObjectURL 来创建本地 URL
      this.form.pictureUrl = URL.createObjectURL(file.raw); // 获取本地图片 URL
      console.log("formdatapictural", this.form.pictureUrl)
      this.form.avatarFile = file.raw; // 保存文件内容
      console.log(this.form.avatarFile)
    },

    /** 上传视频部分 */
    handleAvatarSuccess2(response, file) {
      // 上传成功后的回调
      this.form.videoFile = URL.createObjectURL(file.raw);
      this.form.videoUrl = response.data.url; // 假设返回的response中有文件的URL
    },
    /** 上传前的处理 */
    beforeAvatarUpload2(file) {
      const isVideo = file.type.startsWith("video/");
      const isLt80M = file.size / 1024 / 1024 < 80; // 限制文件大小为 80MB

      if (!isVideo) {
        this.$message.error("上传的文件必须是视频格式!");
      }

      if (!isLt80M) {
        this.$message.error("上传的视频大小不能超过 80MB!");
      }

      if (this.avatarList.length >= 1) {
        this.$message.error("只允许上传一个视频!");
        return false; // 阻止上传
      }

      return isImage && isLt80M;
    },
    /** 视频变化时的处理函数 */
    handleAvatarChange2(file) {
      // 使用 URL.createObjectURL 来创建本地 URL
      this.form.videoUrl = URL.createObjectURL(file.raw); // 获取本地图片 URL
      console.log("formdatavideoUrl", this.form.videoUrl)
      this.form.videoFile = file.raw; // 保存文件内容
      console.log(this.form.videoFile)
    },
  }
};
</script>
<style>
/* 图片上传 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
